/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import EntityState.ItemState;
import EntityState.ReservationState;
import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author James
 */
public class Cart implements Serializable {
    private List<CartItem> listOfCartItems;
    private int            numOfCartItems;
    private int            number;
    
    public Cart() {
        this.listOfCartItems = new LinkedList<CartItem>();
        this.numOfCartItems  = 0;
        this.number          = 0;
    }
    
    public void addItem(ItemState theItem, int quantity) {
        CartItem ci = new CartItem(number++, quantity, theItem, null);
        listOfCartItems.add(ci);
        numOfCartItems++;
    }
    
    public void addReservation(ReservationState theReservation) {
        CartItem ci = new CartItem(number++, 0, null, theReservation);
        listOfCartItems.add(ci);
        numOfCartItems++;
    }
    
    public void updateQuantity(int id, int quantity) {
        for (CartItem item : listOfCartItems) {
            if (item.id == id && item.theItem != null) {
                item.quantity = quantity;
                break;
            }
        }
    }
    
    public void deleteItem(int id) {
        Iterator<CartItem> iter = listOfCartItems.iterator();
        while (iter.hasNext()) {
            CartItem ci = iter.next();
            if (ci.id == id) {
                iter.remove();
                numOfCartItems--;
                break;
            }
        }
    }
    
    public List<CartItem> getListOfCartItems() {
        return listOfCartItems;
    }
    
    public class CartItem {
        public int       id;
        public int       quantity;
        public ItemState theItem;
        public ReservationState theReservation;
        
        public CartItem(int id, int quantity, ItemState theItem, ReservationState theReservation) {
            this.id             = id;
            this.quantity       = quantity;
            this.theItem        = theItem;
            this.theReservation = theReservation;
        }
    }
}
